//
//  UIWebLayer.m
//  Dolphin
//
//  Created by Matthew on 11-7-9.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DolphinUIWebLayer.h"

@implementation DolphinUIWebLayer

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self.backgroundColor = [UIColor whiteColor];
        
        [[NSURLCache sharedURLCache] setMemoryCapacity: 0];
        
        UIView *topBgBar2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height+100)];
        topBgBar2.backgroundColor = [UIColor whiteColor];
        //[self addSubview:topBgBar2];
        //[topBgBar2 release];
        
        // Initialization code
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, frame.size.width-20, frame.size.height)];
        webView.backgroundColor = [UIColor clearColor];
        webView.opaque = TRUE;
        //[webView setOpaque:TRUE];
    
        webView.delegate = self;
        webView.scalesPageToFit = TRUE;
        [self addSubview:webView];
        
        //init the up toolbar
        /*
        if ([IPhone_IPad_Judge isIPad]) 
        {
            upsideToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,frame.size.width,50)];
        }
        else
        {
            upsideToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,frame.size.width,35)];
        }
            
        upsideToolBar.barStyle = UIBarStyleBlackOpaque;// UIBarStyleBlackTranslucent;
        //upsideToolBar.alpha = 0.6;
        [self addSubview: upsideToolBar];
        */
        
        /*
        exitButton =  [ImageUIButton buttonWithType:UIButtonTypeCustom];
        [exitButton  addButtonWithImageLink:@"exit_btn_normal.png" downStatusImgLink:@"exit_btn_on.png"];
        [exitButton  setXY:10 offy:0];
        [exitButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        [self addSubview:exitButton];  

        CGFloat scale = (upsideToolBar.frame.size.height*0.7)/exitButton.frame.size.height;
        exitButton.frame = CGRectMake(10, upsideToolBar.frame.size.height*0.15,exitButton.frame.size.width*scale,exitButton.frame.size.height*scale);
        */
        
        //the loading animation
        activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activityIndicator.frame = CGRectMake(self.frame.size.width/2-20, 
                                                                     (self.frame.size.height+upsideToolBar.frame.size.height)/2-20, 40.0, 40.0);
        [activityIndicator startAnimating];
        [self addSubview:activityIndicator];
    }

    return self;
}

-(void) refreshPage:(NSString *) inLink
{
    NSString *urlAddress = inLink;
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:60.0];
    webView.alpha = 0.0f;
    [webView loadRequest:req];
}

-(void) refreshPageWithHtmlString:(NSString *)inStr
{
     [webView loadHTMLString:inStr baseURL:Nil];
}

-(void) mouseDownProc:(id)sender
{
    //clean up content
    [webView stringByEvaluatingJavaScriptFromString:@"document.open();document.close()"];
    
    if (webView) 
    {
        webView.delegate = Nil;
        [webView removeFromSuperview];
        [webView release];
        
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(webView.frame.origin.x,webView.frame.origin.y, self.frame.size.width, self.frame.size.height-50)];
        webView.backgroundColor = [UIColor whiteColor];
        webView.delegate = self;
        webView.scalesPageToFit = TRUE;
        [self addSubview:webView];

        [self addSubview: upsideToolBar];
        [self addSubview:exitButton];  
        [self addSubview:activityIndicator];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"close_link" object:self];
}

-(void) showLoadingCircle
{
    [activityIndicator startAnimating];
}

- (void)webViewDidStartLoad:(UIWebView *)inWebView 
{
    webView.alpha = 0.0f;
    activityIndicator.hidden = FALSE;
	[activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)inWebView 
{
    webView.alpha = 1.0f;
    [activityIndicator stopAnimating];
    activityIndicator.hidden = YES;
}

-(void) layoutAdjustWithOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    CGRect screen_rect = [[UIScreen mainScreen] bounds];
    
    if(interfaceOrientation == UIInterfaceOrientationPortrait || 
       interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        upsideToolBar.frame = CGRectMake(0, 0, screen_rect.size.width, upsideToolBar.frame.size.height);
        webView.frame = CGRectMake(0, 50, screen_rect.size.width, screen_rect.size.height-50);
        activityIndicator.frame = CGRectMake( screen_rect.size.width/2-20, (screen_rect.size.height+upsideToolBar.frame.size.height)/2-20, 40.0, 40.0);
    }
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        upsideToolBar.frame = CGRectMake(0, 0, screen_rect.size.height, upsideToolBar.frame.size.height);
        webView.frame = CGRectMake(0, 50, screen_rect.size.height, screen_rect.size.width-50);
        activityIndicator.frame = CGRectMake(screen_rect.size.height/2-20,(screen_rect.size.width+upsideToolBar.frame.size.height)/2-20, 40.0, 40.0);
    }
}

-(void) reset
{
    if(exitButton != Nil)
    {
        [exitButton removeTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        [exitButton setUserInteractionEnabled:NO];
        [exitButton removeFromSuperview];
    }
    
    if (webView) 
    {
        webView.delegate = Nil;
        [webView removeFromSuperview];
        [webView release];
    }
    
    if(activityIndicator)
    {
        [activityIndicator release];
        [activityIndicator removeFromSuperview];
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
